#include<bits/stdc++.h>
#define int long long
#define Max 11451419
using namespace std;
int n,m;
int s1[30],s2[30],dp[2010][2010];
char s[2010];
struct node
{
	char t;
	int x,y;
}a[30];
signed main()
{
	freopen("hw.in","r",stdin);
	freopen("hw.out","w",stdout);
	cin>>n>>m;cin>>(s+1);
	for(int i=1;i<=n;i++)
	{
		cin>>a[i].t>>a[i].x>>a[i].y;
		s1[a[i].t-'a']=a[i].x;
		s2[a[i].t-'a']=a[i].y;
	}
	for(int i=1;i<=m;i++)
	{
		for(int j=1;j<=m;j++) dp[i][j]=Max;
		dp[i][i]=0;
		if(s[i]==s[i+1]) dp[i][i+1]=0;
		else dp[i][i+1]=min(min(s1[s[i]-'a'],s2[s[i]-'a']),min(s1[s[i+1]-'a'],s2[s[i+1]-'a']));
	}
	for(int len=3;len<=m;len++)
	{
		for(int l=1;l+len-1<=m;l++)
		{
			int r=l+len-1;
			if(s[l]==s[r]) dp[l][r]=dp[l+1][r-1];
			else
			{
				dp[l][r]=min(dp[l][r],dp[l+1][r]+s1[s[l]-'a']);
				dp[l][r]=min(dp[l][r],dp[l+1][r]+s2[s[l]-'a']);
				dp[l][r]=min(dp[l][r],dp[l][r-1]+s1[s[r]-'a']);
				dp[l][r]=min(dp[l][r],dp[l][r-1]+s2[s[r]-'a']);
			 } 
		}
	}
	cout<<dp[1][m];
	return 0;
}
